﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HRWEB.Controllers
{
    [Authorize(Roles="Admin")]
    public class MagazineController : Controller
    {

        hrdbEntities ent = new hrdbEntities();
        public ActionResult Index()
        {
            return View(new List<Magazine>());
        }
        public ActionResult ListMagazines(string name,string serialNO)
        {
            if (string.IsNullOrEmpty(name))
                return View("Index", new List<Magazine>());
            Session["SelectedMagazineNO"] = serialNO;
            Session["MagazineSearchName"] = name;
            var mags = ent.Magazines.Where(p => p.Name.Contains(name));
            ViewBag.Name = name;
            ViewBag.SelectedMagazineNO = serialNO;
            return View("Index", mags);
        }
        public ActionResult SelectMagazine(string serialNO,string name)
        {
            ViewBag.SelectedMagazineNO = serialNO;
            return RedirectToAction("ListMagazines", new { name = name, serialNO = serialNO });
        }
        public ActionResult DeleteMagazine(string serialNO, string name)
        {
            int s=int.Parse(serialNO);
            var m = ent.Magazines.Where(p => p.SerialNO == s).Single();
            ent.Magazines.Remove(m);
            ent.SaveChanges();
            return RedirectToAction("ListMagazines", new { name = name });
        }
        
        public ActionResult CreateMagazine()
        {
            return PartialView(ent.Magazines.Create());
        }
        [HttpPost]
        public ActionResult RealCreateMagazine(Magazine m)
        {
            ent.Magazines.Add(m);
            ent.SaveChanges();
            return RedirectToAction("ListMagazines", new { name = m.Name });
        }
        public ActionResult CreateMagazineInflunceFactor(string name)
        {
            return PartialView(ent.MagazineInflunceFactors.Create());
        }
        [HttpPost]
        public ActionResult RealCreateMagazineInflunceFactor(MagazineInflunceFactor mif)
        {
            string serialNO = Session["SelectedMagazineNO"].ToString();
            string name = Session["MagazineSearchName"].ToString();
            if (!string.IsNullOrEmpty(serialNO))
            {
                mif.MSerialNO = int.Parse(serialNO);
                ent.MagazineInflunceFactors.Add(mif);
                ent.SaveChanges();
            }
            return RedirectToAction("ListMagazines", new { name = name, serialNO = serialNO });
        }
    }
}
